CLASS lhc_online_shop DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.

    METHODS get_global_features FOR GLOBAL FEATURES
      IMPORTING REQUEST requested_features FOR online_shop RESULT result.

    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR online_shop RESULT result.

    METHODS print FOR MODIFY
      IMPORTING keys FOR ACTION online_shop~print.

    METHODS calculate_order_id FOR DETERMINE ON MODIFY
      IMPORTING keys FOR online_shop~calculate_order_id.
    METHODS button2 FOR MODIFY
      IMPORTING keys FOR ACTION online_shop~button2.

ENDCLASS.

CLASS lhc_online_shop IMPLEMENTATION.

  METHOD get_global_features.
  ENDMETHOD.

  METHOD get_instance_authorizations.
  ENDMETHOD.

  METHOD print.

*  TRY.
*
*        DATA(lo_fdp_util) = cl_fp_fdp_services=>get_instance( 'ZSD_ONLINE_SHOP_01' ).
*
*        DATA(lv_schema) = lo_fdp_util->get_xsd(  ).
*
*        DATA(lv_schema_xml) =  cl_abap_conv_codepage=>create_in( )->convert( lv_schema ).
*
*        DATA(lt_keys)  = lo_fdp_util->get_keys( ).
*
*        SELECT SINGLE order_uuid FROM zonlineshop_001 INTO @DATA(lv_uuid).
*
*        lt_keys[ name = 'ORDER_UUID' ]-value = lv_uuid.
*
*        DATA(lv_xml) = lo_fdp_util->read_to_xml( lt_keys ).
*
*        DATA(lv_xml_out) =  cl_abap_conv_codepage=>create_in( )->convert( lv_xml ).
*
*        DATA lv_string TYPE string.
*
*        SELECT SINGLE * FROM zformtemplate_01
*                        WHERE formid = 'ZDEMOTEST'
*                        INTO @DATA(ls_form).
*
*        cl_fp_ads_util=>render_4_pq(
*          EXPORTING
*            iv_locale       = 'en_US'
*            iv_pq_name      = 'DEVTEST_01' "<= Name of the print queue where result should be stored
*            iv_xml_data     = lv_xml
*            iv_xdp_layout   = ls_form-form_binary
*            is_options      = VALUE #(
*              trace_level = 4 "Use 0 in production environment
*            )
*          IMPORTING
*            ev_trace_string = DATA(lv_trace)
*            ev_pdl          = DATA(lv_pdf)
*        ).
*
*        DATA docname TYPE c LENGTH 120.
*        docname = 'OnlieShopOrder_' && '1'.
*
*        cl_print_queue_utils=>create_queue_item_by_data(
*            "Name of the print queue where result should be stored
*            iv_qname = 'DEVTEST_01'
*            iv_print_data = lv_pdf
*            iv_name_of_main_doc = docname
*        ).
*
*      CATCH cx_root INTO DATA(lv_1).
*        DATA(lv_error) = lv_1->get_longtext( ).
*    ENDTRY.


   DATA lo_http_client  TYPE REF TO if_web_http_client.
    DATA lv_payload TYPE string.

    TRY.
        DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*Should be your communication scenario
          comm_scenario = 'ZPRINT_01'
          comm_system_id = 'ZPRINT2_01' ).

        DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).

        DATA(req) = http_client->get_http_request(  ).

        req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
        req->set_header_field( i_name = 'Accept' i_value = 'application/json' ).

        LOOP AT keys INTO DATA(ls_key).
          lv_payload = '{"Order_Uuid":"' && ls_key-order_uuid && '"}'.
          req->set_text( lv_payload ).
          DATA(lo_response) = http_client->execute( if_web_http_client=>post ).
          DATA(lv_input) = lo_response->get_text( ).
        ENDLOOP.
      CATCH cx_root INTO DATA(e).

        DATA(error) = e->get_longtext( ).

    ENDTRY.

  ENDMETHOD.

  METHOD calculate_order_id.
  DATA:
      online_shops TYPE TABLE FOR UPDATE zi_online_shop_01,
      online_shop  TYPE STRUCTURE FOR UPDATE zi_online_shop_01.

    READ ENTITIES OF zi_online_shop_01 IN LOCAL MODE
       ENTITY online_shop
        ALL FIELDS
          WITH CORRESPONDING #( keys )
          RESULT DATA(lt_online_shop_result)
      FAILED    DATA(lt_failed)
      REPORTED  DATA(lt_reported).
    DATA(today) = cl_abap_context_info=>get_system_date( ).

    LOOP AT lt_online_shop_result INTO DATA(online_shop_read).
      online_shop               = CORRESPONDING #( online_shop_read ).
      IF online_shop-orderid IS INITIAL.
        TRY.
            CALL METHOD cl_numberrange_runtime=>number_get
              EXPORTING
                nr_range_nr = '01'
                object      = 'ZOSOID_01'
              IMPORTING
                number      = DATA(order_id)
                returncode  = DATA(rcode).
          CATCH cx_nr_object_not_found.
          CATCH cx_number_ranges.
        ENDTRY.
        online_shop-orderid = CONV int4( order_id ).
        online_shop-creationdate  = today.
        IF online_shop-deliverydate IS INITIAL.
          online_shop-deliverydate  = today + 10.
        ENDIF.

        APPEND online_shop TO online_shops.
      ENDIF.
    ENDLOOP.

    MODIFY ENTITIES OF zi_online_shop_01 IN LOCAL MODE
    ENTITY zi_online_shop_01 UPDATE SET FIELDS WITH online_shops
    MAPPED   DATA(ls_mapped_modify)
    FAILED   DATA(lt_failed_modify)
    REPORTED DATA(lt_reported_modify).


  ENDMETHOD.

  METHOD button2.
  data lv_point TYPE c.
  lv_point = '1'.
  ENDMETHOD.

ENDCLASS.
